package com.fnzx.controller;

import java.io.IOException;
import java.util.Arrays;
import java.util.Map;

import com.fnzx.entity.VideoEntity;
import com.fnzx.service.VideoService;
import com.fnzx.util.PageUtils;
import com.fnzx.util.R;
import com.fnzx.util.oss.OssUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

/**
 * 
 *
 * @author chenshun
 * @email sunlightcs@gmail.com
 * @date 2021-10-07 10:54:12
 */
@RestController
@RequestMapping("iot/video")
@Api(tags ="视频模块")
public class VideoController {
    @Autowired
    private VideoService videoService;

    /**
     * 列表
     */
    @ApiOperation(value ="获取所有视频" )
    @GetMapping("/list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = videoService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @ApiOperation(value = "后台页面--查询视频",notes = "根据视频id查询视频")
    @GetMapping("/info/{id}")
    public R info(@PathVariable("id") Long id){
		VideoEntity video = videoService.getById(id);

        return R.ok().put("video", video);
    }

    /**
     * 保存
     */
    @ApiOperation(value = "后台页面--添加视频",notes = "添加视频{\"url\":,\"title\":}")
    @PostMapping("/save")
    public R save(@RequestBody VideoEntity video,
                  @RequestHeader String token){
		videoService.save(video);

        return R.ok();
    }

    /**
     * 修改
     */
    @ApiOperation(value = "后台页面--修改视频",notes = "修改视频{\"id\":,\"url\":,\"title\":}")
    @PostMapping("/update")
    public R update(@RequestBody VideoEntity video,
                    @RequestHeader String token){
		videoService.updateById(video);

        return R.ok();
    }

    /**
     * 删除
     */
    @ApiOperation(value = "后台页面--删除视频",notes = "根据视频id删除视频[id1,id2,……]")
    @PostMapping("/delete")
    public R delete(@RequestBody Long[] ids,
                    @RequestHeader String token){
		videoService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

    //上传文件（图片和视频）到阿里云oss
    @ApiOperation(value = "后台管理系统--上传视频", notes = "上传文件（图片和视频）到阿里云oss")
    @PostMapping("/videoUpload")
    public R fileUpload(MultipartFile file,@RequestHeader String token) throws IOException {
        //获取上传文件
        String url = OssUtils.upload(file);
        return R.ok(url);
    }

}
